﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Hjp.FrameWork.Common;
using Wjtour.BLL;

namespace Wjtour.Web.Admin
{
    public partial class login : System.Web.UI.Page
    {
        protected TextBox username;
        protected TextBox password;
        protected TextBox yzm;
        protected ImageButton loginBtn;

        protected void Page_Load(object sender, EventArgs e)
        {
            loginBtn.Click += new ImageClickEventHandler(loginBtn_Click);
        }

        void loginBtn_Click(object sender, ImageClickEventArgs e)
        {
            string name =  this.username.Text.Trim();
            string pwd = this.password.Text.Trim();
            string code = this.yzm.Text.Trim().ToLower();
            if (Session["CheckCode"] == null || code != Session["CheckCode"].ToString().ToLower())
            {
                Utils.Alert(this, "验证码错误!");
                yzm.Text = "";
                return;
            }

            sysadmin_bll bll = new sysadmin_bll();
            var user = bll.login(name, pwd);
            if (user != null)
            {
                Session["CurrentUser"] = user;

                user.lastloginip = user.loginip;
                user.lastlogintime = user.logintime;
                user.loginip = Utils.GetRealIP();
                user.loginnum += 1;
                user.logintime = DateTime.Now;
                bll.Update(user);

                Wjtour.Domain.adminlog log = new Domain.adminlog();
                log.admin_name = user.adminname;
                log.done = "login";
                log.ip = Utils.GetRealIP();
                log.mod = "login";
                log.addtime = DateTime.Now;
                new Wjtour.BLL.adminlog_bll().Save(log);

                Response.Redirect("index.aspx");
            }
            else
            {
                Utils.Alert(this, "请确认用户名或密码是否正确!");
            }
        }
    }
}